﻿using Heyo.Controls.Class;
using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Heyo.Controls
{
    public class ColorBackground : Canvas
    {
        private readonly DispatcherTimer _updateTimer = new DispatcherTimer();
        private readonly DispatcherTimer _addRemoveTimer = new DispatcherTimer();
        public int GrainsCount { get; set; } = 12;
        public double Acceleration { get; set; } = 4;
        public int MinSize { get; set; } = 150;
        public int MaxSize { get; set; } = 300;
        public bool AutoStart { get; set; } = true;

        private readonly RadialGradientBrush _opacityMask
            = new RadialGradientBrush(
                new GradientStopCollection() {
                    new GradientStop(Color.FromArgb(180 , 0, 0, 0),0),
                    new GradientStop(Color.FromArgb(0, 0, 0, 0), 1),
                    new GradientStop(Color.FromArgb(150, 0, 0, 0), 0.2),
                    new GradientStop(Color.FromArgb(16, 0, 0, 0), 0.7),
                });
        public ColorBackground()
        {
            UseLayoutRounding = false;
            SnapsToDevicePixels = false;
            ClipToBounds = true;
            Background = new SolidColorBrush(Color.FromArgb(1, 255, 255, 255));
            _addRemoveTimer.Tick += AddRemoveTimer_Tick;
            _addRemoveTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000 * 5);
            _updateTimer.Tick += DrawingAy;
            _updateTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000 / 30);

            Loaded += GrainsBackground_Loaded;

        }

        private void AddRemoveTimer_Tick(object sender, EventArgs e)
        {
            int i = _rand.Next(0, _grains.Count);
            _grains.RemoveAt(i);
            _ellipses.RemoveAt(i);
            AddGrains();
        }

        public void Stop()
        {
            _updateTimer.Stop();
            //addRemoveTimer.Stop();
        }
        public void Start()
        {
            _updateTimer.Start();
            //addRemoveTimer.Start();
        }

        private void GrainsBackground_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (AutoStart)
            {
                Start();
            }
            //// 添加粒子
            //// x，y为粒子坐标，xa, ya为粒子xy轴加速度，max为连线的最大距离     
            for (var i = 0; i < GrainsCount; i++)
            {
                AddGrains();
            }

        }
        private void AddGrains()
        {
            int size = _rand.Next(MinSize, MaxSize);
            var gb = new GrainBase
            {
                X = (_rand.NextDouble() * ActualWidth - 2 * size) + size,
                Y = (_rand.NextDouble() * ActualHeight - 2 * size) + size,
                Xa = _rand.NextDouble() * Acceleration - 1,
                Ya = _rand.NextDouble() * Acceleration - 1,
                Size = size,
                Color = GetColor(_rand.Next(0, 6))
            };
            var elip = new Ellipse
            {
                OpacityMask = _opacityMask
            };
            elip.Height = elip.Width = gb.Size*2;
            elip.Fill = new SolidColorBrush(gb.Color);
            _ellipses.Add(elip);
            _grains.Add(gb);
        }

        private void DrawingAy(object sender, EventArgs e)
        {
            Next();
        }


        private readonly List<GrainBase> _grains = new List<GrainBase>();
        private readonly List<Ellipse> _ellipses = new List<Ellipse>();
        private readonly Random _rand = new Random();

        public void Next()
        {
            Children.Clear();

            for (var i = 0; i < _grains.Count; i++)
            {
                GrainBase grain = _grains[i];
                if (grain.X == null || grain.Y == null) continue;
                // 粒子位移
                grain.X += grain.Xa;
                grain.Y += grain.Ya;
                // 遇到边界将加速度反向
                grain.Xa *= (grain.X.Value + grain.Size > ActualWidth || grain.X.Value + grain.Size < 0) ? -1 : 1;
                grain.Ya *= (grain.Y.Value + grain.Size > ActualHeight || grain.Y.Value + grain.Size < 0) ? -1 : 1;
                // 绘制点
                Ellipse elip = _ellipses[i];

                SetLeft(elip, grain.X.Value);
                SetTop(elip, grain.Y.Value);
                Children.Add(elip);
            }
        }

        private static readonly List<Color> Colors = new List<Color>()
        {
            System.Windows.Media.Colors.LightBlue,
            System.Windows.Media.Colors.Gold,
            System.Windows.Media.Colors.PaleGreen,
            System.Windows.Media.Colors.LightCoral,
            System.Windows.Media.Colors.HotPink,
            System.Windows.Media.Colors.Plum
        };
        public static Color GetColor(int color)
        {
            return Colors[color];
        }
    }
}
